package sk.uniba.fmph.asdf.gui.frames;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;
import sk.uniba.fmph.asdf.Exporter;
import sk.uniba.fmph.asdf.Session;
import sk.uniba.fmph.asdf.exceptions.DatabaseException;
import sk.uniba.fmph.asdf.exceptions.OfflineException;
import sk.uniba.fmph.asdf.tempt.PrimaryTestFieldDef;
import sk.uniba.fmph.asdf.tempt.TestFieldDef;
import sk.uniba.fmph.asdf.tempt.TestTemplate;
import sk.uniba.fmph.asdf.test.Comment;
import sk.uniba.fmph.asdf.test.PrimaryTestField;
import sk.uniba.fmph.asdf.test.RelativePoint;
import sk.uniba.fmph.asdf.test.Stamp;
import sk.uniba.fmph.asdf.test.StampType;
import sk.uniba.fmph.asdf.test.Test;
import sk.uniba.fmph.asdf.utils.Convertor;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.SftpException;
import javax.swing.JOptionPane;
import sk.uniba.fmph.asdf.gui.views.MainView;

/**
 *
 * @author Jakub Gaľ
 * @author Matej Ulicny
 */
public class EvaluationFrame extends Frame {

	private static final long serialVersionUID = -2163141462199519613L;
	private Test test = null;
	private Image image = null;
	private Image alt, ecf, ok, sd, seen, comment, ext, scaledAlt, scaledEcf,
					scaledOk, scaledSd, scaledSeen, scaledComment, scaledExt;
	StampType stampType;
	Point cursorPoint;
	RelativePoint initialPoint, extensionPoint;
	private final String fontFamily = "Verdana";
	private javax.swing.JButton buttonALT;
	private javax.swing.JButton buttonCorrect;
	private javax.swing.JButton buttonECF;
	private javax.swing.JButton buttonExtension;
	private javax.swing.JButton buttonSD;
	private javax.swing.JButton buttonSeen;
	private javax.swing.JButton buttonComment;
	private javax.swing.JButton buttonDelete;
	private javax.swing.JButton buttonSave;
	private javax.swing.JLabel labelPointsTotal;
	private javax.swing.JLabel labelPointsTotalValue;
	private javax.swing.JTextArea textAreaComments;
	private javax.swing.JPanel panelCenter;
	private javax.swing.JPanel panelLeft;
	private javax.swing.JPanel panelRight;
	private javax.swing.JPanel panelTestPreview;
	private javax.swing.JScrollPane scrollPaneComments;
	private javax.swing.JScrollPane scrollPaneTestPreview;
	private javax.swing.JScrollPane scrollPaneTests;
	private javax.swing.JTree treeTests;

	/**
	 * Vytvori novy formular evaluationView
	 */
	public EvaluationFrame(MainView parent) {
		super(parent);
		initComponents();
	}

	@Override
	public void onShow() {
		// parent.setSubtitle("Hodnotenie testov");
	}

	/**
	 * Inicializuje gui komponenty
	 */
	private void initComponents() {
		setMinimumSize(new java.awt.Dimension(800, 600));
		setPreferredSize(new java.awt.Dimension(800, 600));
		setLayout(new javax.swing.BoxLayout(this,
						javax.swing.BoxLayout.LINE_AXIS));

		cursorPoint = new Point(-1, -1);
		initialPoint = null;
		extensionPoint = null;
		panelLeft = new javax.swing.JPanel();
		scrollPaneTests = new javax.swing.JScrollPane();
		treeTests = new javax.swing.JTree();
		treeTests.addTreeSelectionListener(new TreeSelectionListener() {
			@Override
			public void valueChanged(TreeSelectionEvent e) {
				DefaultMutableTreeNode node = (DefaultMutableTreeNode) treeTests
								.getLastSelectedPathComponent();
				if (node != null && (node.getUserObject() instanceof Test)) {
					test = (Test) node.getUserObject();
					image = test.getImage().getScaledInstance(
									panelTestPreview.getWidth()
									- scrollPaneTestPreview
									.getVerticalScrollBar().getWidth(),
									-1, Image.SCALE_SMOOTH);
					panelTestPreview.setPreferredSize(new Dimension(
									panelTestPreview.getWidth(), image.getHeight(null)));
					scaledAlt = alt.getScaledInstance(
									image.getWidth(null) / 10, -1, Image.SCALE_SMOOTH);
					scaledEcf = ecf.getScaledInstance(
									image.getWidth(null) / 10, -1, Image.SCALE_SMOOTH);
					scaledOk = ok.getScaledInstance(image.getWidth(null) / 15,
									-1, Image.SCALE_SMOOTH);
					scaledSd = sd.getScaledInstance(image.getWidth(null) / 10,
									-1, Image.SCALE_SMOOTH);
					scaledSeen = seen.getScaledInstance(
									image.getWidth(null) / 10, -1, Image.SCALE_SMOOTH);
					scaledComment = comment.getScaledInstance(
									image.getWidth(null) / 10, -1, Image.SCALE_SMOOTH);
					scaledExt = ext.getScaledInstance(
									image.getWidth(null) / 10, -1, Image.SCALE_SMOOTH);
					repaint();
					scrollPaneTestPreview.revalidate();
				}
			}
		});
		panelCenter = new javax.swing.JPanel();
		scrollPaneTestPreview = new javax.swing.JScrollPane();
		panelTestPreview = new javax.swing.JPanel() {
			private static final long serialVersionUID = 7558708239399276113L;

			/**
			 * {@inheritDoc}
			 */
			@Override
			public void paint(Graphics g) {
				g.clearRect(0, 0, g.getClipBounds().width,
								g.getClipBounds().height);
				if (test != null) {

					if (test.getImage() != null) {
						g.drawImage(image, 0, 0, null);
						Font font = new Font(fontFamily, Font.PLAIN, 23);
						g.setFont(font);
						// draw fields
						for (PrimaryTestField field : test.getFields()) {
							PrimaryTestFieldDef f = field.getTestFieldDef();
							Point tl = new Point(
											Math.round((f.getTopLeft().x / 100)
											* image.getWidth(null)),
											Math.round((f.getTopLeft().y / 100)
											* image.getHeight(null)));
							Point br = new Point(
											Math.round((f.getBottomRight().x / 100)
											* image.getWidth(null)),
											Math.round((f.getBottomRight().y / 100)
											* image.getHeight(null)));

							g.setColor(new Color(0, 255, 0, 20));
							g.fillRect(tl.x, tl.y, br.x - tl.x, br.y - tl.y);
							g.setColor(Color.green);
							g.drawRect(tl.x, tl.y, br.x - tl.x, br.y - tl.y);
							String points = String.valueOf(field
											.getEvaluation() + "/" + f.getMaxPoints());
							g.setColor(new Color(0, 0, 0, 100));
							int w = g.getFontMetrics().stringWidth(points);
							int h = g.getFontMetrics().getHeight();
							g.drawString(points, br.x - w, tl.y + h / 2 + 3);
							// extension
							if (field.getFieldExtension() != null) {
								TestFieldDef ex = field.getFieldExtension();
								Point extl = new Point(Math.round((ex
												.getTopLeft().x / 100)
												* image.getWidth(null)), Math.round((ex
												.getTopLeft().y / 100)
												* image.getHeight(null)));
								Point exbr = new Point(Math.round((ex
												.getBottomRight().x / 100)
												* image.getWidth(null)), Math.round((ex
												.getBottomRight().y / 100)
												* image.getHeight(null)));

								g.setColor(new Color(150, 255, 0, 20));
								g.fillRect(extl.x, extl.y, exbr.x - extl.x,
												exbr.y - extl.y);
								g.setColor(new Color(150, 255, 0));
								g.drawRect(extl.x, extl.y, exbr.x - extl.x,
												exbr.y - extl.y);
								g.drawImage(scaledExt, extl.x, extl.y, null);
								g.setColor(new Color(0, 0, 0, 100));
								g.drawString(points, exbr.x - w, extl.y + h / 2
												+ 3);
							}
						}
						// imaginary extension stamp
						if (extensionPoint != null) {
							Point exStamp = new Point(
											Math.round((extensionPoint.x / 100)
											* image.getWidth(null)),
											Math.round((extensionPoint.y / 100)
											* image.getHeight(null)));
							g.drawImage(scaledExt,
											exStamp.x - scaledExt.getWidth(null) / 2,
											exStamp.y - scaledExt.getHeight(null) / 2,
											null);
						}
						// actual extension
						if (initialPoint != null) {
							Point tl = new Point(Math.min(
											cursorPoint.x,
											Math.round((initialPoint.x / 100)
											* image.getWidth(null))), Math.min(
											cursorPoint.y,
											Math.round((initialPoint.y / 100)
											* image.getHeight(null))));
							Point br = new Point(Math.max(
											cursorPoint.x,
											Math.round((initialPoint.x / 100)
											* image.getWidth(null))), Math.max(
											cursorPoint.y,
											Math.round((initialPoint.y / 100)
											* image.getHeight(null))));
							g.setColor(new Color(130, 255, 0, 20));
							g.fillRect(tl.x, tl.y, br.x - tl.x, br.y - tl.y);
							g.setColor(new Color(130, 255, 0));
							g.drawRect(tl.x, tl.y, br.x - tl.x, br.y - tl.y);
						}
						// draw stamps
						for (PrimaryTestField f : test.getFields()) {
							LinkedList<Stamp> stamps = (LinkedList<Stamp>) f
											.getStamps();
							for (Stamp s : stamps) {
								Point tl = new Point(Math.round((s
												.getPosition().x / 100)
												* image.getWidth(null)), Math.round((s
												.getPosition().y / 100)
												* image.getHeight(null)));
								Image stampImage = null;
								if (s.getStampType() == StampType.CORRECT) {
									stampImage = scaledOk;
								} else if (s.getStampType() == StampType.ERROR_CARRIED_FORWARD) {
									stampImage = scaledEcf;
								} else if (s.getStampType() == StampType.SEEN) {
									stampImage = scaledSeen;
								} else if (s.getStampType() == StampType.SIGNIFICANT_DIGITS) {
									stampImage = scaledSd;
								} else if (s.getStampType() == StampType.ALTERNATIVE_ANSWER) {
									stampImage = scaledAlt;
								} else if (s.getStampType() == StampType.EXTENSION) {
									stampImage = scaledExt;
								} else if (s.getStampType() == StampType.COMMENT) {
									stampImage = scaledComment;
								}
								if (stampImage != null) {
									if (test.getSelectedStamps().contains(s)) {
										g.setColor(new Color(100, 100, 255, 50));
										g.fillRect(
														tl.x
														- (stampImage
														.getWidth(null) / 2)
														- 2,
														tl.y
														- (stampImage
														.getHeight(null) / 2)
														- 2, stampImage
														.getWidth(null) + 4,
														stampImage.getHeight(null) + 4);
									}
									g.drawImage(stampImage,
													tl.x - stampImage.getWidth(null)
													/ 2,
													tl.y - stampImage.getHeight(null)
													/ 2, null);
								}
							}
						}
					}
					/* draw cursor stamp */
					Image stampImage = null;
					if (stampType == StampType.CORRECT) {
						stampImage = scaledOk;
					} else if (stampType == StampType.ERROR_CARRIED_FORWARD) {
						stampImage = scaledEcf;
					} else if (stampType == StampType.SEEN) {
						stampImage = scaledSeen;
					} else if (stampType == StampType.SIGNIFICANT_DIGITS) {
						stampImage = scaledSd;
					} else if (stampType == StampType.ALTERNATIVE_ANSWER) {
						stampImage = scaledAlt;
					} else if (stampType == StampType.EXTENSION) {
						stampImage = scaledExt;
					} else if (stampType == StampType.COMMENT) {
						stampImage = scaledComment;
					}
					if (stampImage != null && cursorPoint != null
									&& cursorPoint.x != -1) {
						g.drawImage(stampImage,
										cursorPoint.x - stampImage.getWidth(null) / 2,
										cursorPoint.y - stampImage.getHeight(null) / 2,
										null);
						cursorPoint.x = -1;
						cursorPoint.y = -1;
					}
					int gainedPoints = test.getPoints();
					int maxPoints = test.getTemplate().getMaxPoints();
					labelPointsTotalValue.setText(gainedPoints + "/"
									+ maxPoints);
					panelTestPreview.revalidate();
					scrollPaneTestPreview.revalidate();
				}
			}
		};
		scrollPaneTestPreview.addComponentListener(new ComponentListener() {
			@Override
			public void componentResized(ComponentEvent ce) {
				if (test == null) {
					return;
				}

				image = test.getImage()
								.getScaledInstance(
								scrollPaneTestPreview.getWidth()
								- scrollPaneTestPreview
								.getVerticalScrollBar()
								.getWidth() - 3, -1,
								Image.SCALE_SMOOTH);

				scaledAlt = alt.getScaledInstance(image.getWidth(null) / 10,
								-1, Image.SCALE_SMOOTH);
				scaledEcf = ecf.getScaledInstance(image.getWidth(null) / 10,
								-1, Image.SCALE_SMOOTH);
				scaledOk = ok.getScaledInstance(image.getWidth(null) / 15, -1,
								Image.SCALE_SMOOTH);
				scaledSd = sd.getScaledInstance(image.getWidth(null) / 10, -1,
								Image.SCALE_SMOOTH);
				scaledSeen = seen.getScaledInstance(image.getWidth(null) / 10,
								-1, Image.SCALE_SMOOTH);
				scaledComment = comment.getScaledInstance(
								image.getWidth(null) / 10, -1, Image.SCALE_SMOOTH);
				scaledExt = ext.getScaledInstance(image.getWidth(null) / 10,
								-1, Image.SCALE_SMOOTH);

				panelTestPreview.setPreferredSize(new Dimension(image
								.getWidth(null), image.getHeight(null)));

				panelTestPreview.revalidate();
				panelTestPreview.repaint();
			}

			@Override
			public void componentHidden(ComponentEvent arg0) {
			}

			@Override
			public void componentMoved(ComponentEvent arg0) {
			}

			@Override
			public void componentShown(ComponentEvent arg0) {
			}
		});

		panelTestPreview.addMouseListener(new MouseListener() {
			@Override
			public void mouseReleased(MouseEvent arg0) {
			}

			@Override
			public void mousePressed(MouseEvent arg0) {
			}

			@Override
			public void mouseExited(MouseEvent arg0) {
				repaint();
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
			}

			/**
			 * {@inheritDoc}
			 */
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (test == null) {
					return;
				}
				// right click
				if (SwingUtilities.isRightMouseButton(arg0)) {
					stampType = null;
					extensionPoint = null;
					initialPoint = null;
					repaint();
					return;
				}
				Dimension d = new Dimension(image.getWidth(null), image
								.getHeight(null));
				Point p = arg0.getPoint();
				RelativePoint rp = Convertor.absToRel(p, d);
				// extension selection start
				if (extensionPoint != null && initialPoint == null) {
					initialPoint = rp;
					return;
				}
				// add extension
				if (initialPoint != null && extensionPoint != null) {
					RelativePoint tl = new RelativePoint(Math.min(
									initialPoint.x, rp.x), Math.min(initialPoint.y,
									rp.y));
					RelativePoint br = new RelativePoint(Math.max(
									initialPoint.x, rp.x), Math.max(initialPoint.y,
									rp.y));
					if (!isOverlaping(tl, br)) {
						for (PrimaryTestField f : test.getFields()) {
							if (f.getTestFieldDef().containsPoint(
											extensionPoint)) {
								f.addStamp(extensionPoint.x, extensionPoint.y,
												StampType.EXTENSION);
								f.addFieldExtension(tl, br);
							}
						}
					}
					initialPoint = null;
					extensionPoint = null;
					repaint();
					return;
				}
				// if selecting stamp
				if (test.selectStamp(rp.x, rp.y, d)) {
					if (!test.getSelectedStamps().isEmpty()) {
						Stamp s = test.getSelectedStamps().get(
										test.getSelectedStamps().size() - 1);
						if (s.getStampType() == StampType.COMMENT) {
							Comment c = (Comment) s;
							textAreaComments.setText(c.getValue());
						}
					}
					repaint();
				} else {
					if (stampType != null) {
						// add comment
						if (stampType == StampType.COMMENT) {
							test.addComment(new Comment(rp.x, rp.y,
											textAreaComments.getText()));
							repaint();
							textAreaComments.setText("");
						} else { // add stamp
							// add extension
							if (stampType == StampType.EXTENSION) {
								for (PrimaryTestField f : test.getFields()) {
									if (f.getTestFieldDef().containsPoint(rp)
													&& f.getFieldExtension() == null) {
										extensionPoint = new RelativePoint(
														rp.x, rp.y);
										stampType = null;
										repaint();
									}
								}
							} // add another stamp
							else {
								if (test.addStamp(rp.x, rp.y, stampType)) {
									repaint();
								}
							}
						}
					}
				}
			}
		});

		panelTestPreview.addMouseMotionListener(new MouseMotionListener() {
			@Override
			public void mouseMoved(MouseEvent arg0) {
				cursorPoint = arg0.getPoint();
				repaint();
			}

			@Override
			public void mouseDragged(MouseEvent arg0) {
			}
		});

		scrollPaneComments = new javax.swing.JScrollPane();
		textAreaComments = new javax.swing.JTextArea();
		panelRight = new javax.swing.JPanel();
		buttonCorrect = new javax.swing.JButton();
		buttonCorrect.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				stampType = StampType.CORRECT;
				initialPoint = null;
				cursorPoint = null;
				repaint();
			}
		});
		buttonSeen = new javax.swing.JButton();
		buttonSeen.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				stampType = StampType.SEEN;
				initialPoint = null;
				cursorPoint = null;
				repaint();
			}
		});
		buttonECF = new javax.swing.JButton();
		buttonECF.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				stampType = StampType.ERROR_CARRIED_FORWARD;
				initialPoint = null;
				cursorPoint = null;
				repaint();
			}
		});
		buttonSD = new javax.swing.JButton();
		buttonSD.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				stampType = StampType.SIGNIFICANT_DIGITS;
				initialPoint = null;
				cursorPoint = null;
				repaint();
			}
		});
		buttonALT = new javax.swing.JButton();
		buttonALT.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				stampType = StampType.ALTERNATIVE_ANSWER;
				initialPoint = null;
				cursorPoint = null;
				repaint();
			}
		});
		buttonComment = new javax.swing.JButton();
		buttonComment.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				stampType = StampType.COMMENT;
				initialPoint = null;
				cursorPoint = null;
				repaint();
			}
		});
		buttonExtension = new javax.swing.JButton();
		buttonExtension.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				stampType = StampType.EXTENSION;
				initialPoint = null;
				cursorPoint = null;
				repaint();
			}
		});
		buttonDelete = new javax.swing.JButton();
		buttonDelete.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				test.deleteSelectedStamps();
				repaint();
			}
		});
		buttonSave = new javax.swing.JButton();
		buttonSave.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					if(test != null) {
						Session.synchronize();
					}
					else {
						JOptionPane.showMessageDialog(parent, "Neopravujete žiadny test", "Chyba!", JOptionPane.ERROR_MESSAGE);
					}
				} catch (DatabaseException | JSchException | SftpException | IOException | OfflineException e) {
					e.printStackTrace();
				}
			}
		});
		labelPointsTotal = new javax.swing.JLabel();
		labelPointsTotalValue = new javax.swing.JLabel();

		panelLeft.setPreferredSize(new java.awt.Dimension(175, 600));
		panelLeft
						.setMaximumSize(new java.awt.Dimension(175, Integer.MAX_VALUE));
		panelLeft.setLayout(new javax.swing.BoxLayout(panelLeft,
						javax.swing.BoxLayout.PAGE_AXIS));

		generateTestTree();
		scrollPaneTests.setViewportView(treeTests);

		panelLeft.add(scrollPaneTests);

		add(panelLeft);

		panelCenter.setBorder(javax.swing.BorderFactory.createEtchedBorder());
		panelCenter.setPreferredSize(new java.awt.Dimension(450, 600));
		panelCenter.setLayout(new java.awt.BorderLayout());

		scrollPaneTestPreview
						.setPreferredSize(new java.awt.Dimension(450, 500));
		scrollPaneTestPreview.setViewportView(panelTestPreview);
		scrollPaneTestPreview
						.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

		panelCenter.add(scrollPaneTestPreview, BorderLayout.CENTER);

		textAreaComments.setEditable(true);
		textAreaComments.setColumns(20);
		textAreaComments.setFont(new java.awt.Font("DejaVu Sans", 1, 14)); // NOI18N
		textAreaComments.setLineWrap(true);
		textAreaComments.setRows(5);
		textAreaComments.setText(""/* "Poznámky" */);
		textAreaComments.setMaximumSize(new java.awt.Dimension(
						Integer.MAX_VALUE, 100));
		textAreaComments.setPreferredSize(new java.awt.Dimension(450, 100));

		scrollPaneComments.setMaximumSize(new java.awt.Dimension(
						Integer.MAX_VALUE, 100));
		scrollPaneComments.setPreferredSize(new java.awt.Dimension(450, 100));

		scrollPaneComments.add(textAreaComments);

		panelCenter.add(textAreaComments, BorderLayout.SOUTH);

		add(panelCenter);

		panelRight
						.setMaximumSize(new java.awt.Dimension(175, Integer.MAX_VALUE));
		panelRight.setPreferredSize(new java.awt.Dimension(175, 600));
		panelRight.setLayout(new java.awt.GridLayout(0, 1));

		try {
			alt = ImageIO.read(new File("res/stamps/ALT.png"));
			ecf = ImageIO.read(new File("res/stamps/ECF.png"));
			ok = ImageIO.read(new File("res/stamps/OK.png"));
			sd = ImageIO.read(new File("res/stamps/SD.png"));
			seen = ImageIO.read(new File("res/stamps/SEEN.png"));
			comment = ImageIO.read(new File("res/stamps/Comment.png"));
			ext = ImageIO.read(new File("res/stamps/EXT.png"));
		} catch (IOException e1) {
			e1.printStackTrace();
		}

		// buttonCorrect.setText("+1 bod");
		buttonCorrect.setBackground(Color.white);
		buttonCorrect.setIcon(new ImageIcon(ok));
		// buttonSeen.setText("SEEN");
		buttonSeen.setBackground(Color.white);
		buttonSeen.setIcon(new ImageIcon(seen));
		// buttonECF.setText("ECF");
		buttonECF.setBackground(Color.white);
		buttonECF.setIcon(new ImageIcon(ecf));
		// buttonSD.setText("SD");
		buttonSD.setBackground(Color.white);
		buttonSD.setIcon(new ImageIcon(sd));
		// buttonALT.setText("ALT");
		buttonALT.setBackground(Color.white);
		buttonALT.setIcon(new ImageIcon(alt));
		buttonComment.setBackground(Color.white);
		// buttonComment.setText("Komentár");
		buttonComment.setIcon(new ImageIcon(comment));
		buttonExtension.setBackground(Color.white);
		// buttonExtension.setText("Presah");
		buttonExtension.setIcon(new ImageIcon(ext));
		buttonDelete.setText("Vymazať pečiatky");
		buttonDelete.setBackground(Color.white);
		buttonDelete.setFont(new java.awt.Font("DejaVu Sans", 0, 13));
		buttonDelete.setMnemonic(KeyEvent.VK_DELETE);
		buttonSave.setText("Uložiť zmeny");
		buttonSave.setBackground(Color.white);
		buttonSave.setFont(new java.awt.Font("DejaVu Sans", 0, 13));

		labelPointsTotal.setFont(new java.awt.Font("DejaVu Sans", 0, 12));
		labelPointsTotal.setVerticalAlignment(SwingConstants.BOTTOM);
		labelPointsTotal.setHorizontalAlignment(SwingConstants.CENTER);
		labelPointsTotal.setText("Počet bodov");

		labelPointsTotalValue.setFont(new java.awt.Font("DejaVu Sans", 1, 24));
		labelPointsTotalValue.setHorizontalAlignment(SwingConstants.CENTER);
		labelPointsTotalValue.setText("");

		panelRight.add(buttonCorrect);
		panelRight.add(buttonSeen);
		panelRight.add(buttonECF);
		panelRight.add(buttonSD);
		panelRight.add(buttonALT);
		panelRight.add(buttonComment);
		panelRight.add(buttonExtension);
		panelRight.add(buttonDelete);
		panelRight.add(buttonSave);
		panelRight.add(labelPointsTotal);
		panelRight.add(labelPointsTotalValue);

		add(panelRight);
	}

	/**
	 * funkcia zistuje, ci sa novo vytvorena oblast prekryva s nejakou existujucou
	 * oblastou
	 *
	 * @param topLeft lavy horny okrajovy bod novej oblasti
	 * @param bottomRight pravy dolny okrajovy bod novej oblasti
	 * @return true ak sa obslasti prekryvaju, inak false
	 */
	public boolean isOverlaping(RelativePoint topLeft, RelativePoint bottomRight) {
		for (PrimaryTestField field : test.getFields()) {
			if (isContained(topLeft.x, topLeft.y, field.getTestFieldDef()
						.getTopLeft(), field.getTestFieldDef().getBottomRight())
						|| isContained(bottomRight.x, bottomRight.y, field
						.getTestFieldDef().getTopLeft(), field
						.getTestFieldDef().getBottomRight())
						|| isContained(topLeft.x, bottomRight.y, field
						.getTestFieldDef().getTopLeft(), field
						.getTestFieldDef().getBottomRight())
						|| isContained(bottomRight.x, topLeft.y, field
						.getTestFieldDef().getTopLeft(), field
						.getTestFieldDef().getBottomRight())
						|| isContained(field.getTestFieldDef()
						.getTopLeft().x, field.getTestFieldDef()
						.getTopLeft().y, topLeft, bottomRight)
						|| isContained(field.getTestFieldDef().getBottomRight().x,
						field.getTestFieldDef().getBottomRight().y, topLeft, bottomRight)
						|| isContained(field.getTestFieldDef().getTopLeft().x, field
						.getTestFieldDef().getBottomRight().y, topLeft, bottomRight)
						|| isContained(field.getTestFieldDef().getBottomRight().x, field
						.getTestFieldDef().getTopLeft().y, topLeft, bottomRight)) {
				return true;
			}
			if (field.getFieldExtension() != null
					&& (isContained(topLeft.x, topLeft.y, field
						.getFieldExtension().getTopLeft(), field
						.getFieldExtension().getBottomRight())
						|| isContained(bottomRight.x, bottomRight.y, field
						.getFieldExtension().getTopLeft(), field
						.getFieldExtension().getBottomRight())
						|| isContained(topLeft.x, bottomRight.y, field
						.getFieldExtension().getTopLeft(), field
						.getFieldExtension().getBottomRight()) || isContained(
						bottomRight.x, topLeft.y, field
						.getFieldExtension().getTopLeft(),
						field.getFieldExtension().getBottomRight())
						|| isContained(field.getFieldExtension().getTopLeft().x, 
						field.getFieldExtension().getTopLeft().y, topLeft, bottomRight)
						|| isContained(field.getFieldExtension().getBottomRight().x, field
						.getFieldExtension().getBottomRight().y, topLeft, bottomRight)
						|| isContained(field.getFieldExtension().getTopLeft().x, 
						field.getFieldExtension().getBottomRight().y, topLeft, bottomRight) 
						|| isContained(field.getFieldExtension().getBottomRight().x,
						field.getFieldExtension().getTopLeft().y, topLeft, bottomRight))) {
				return true;
			}
		}
		return false;
	}

	/**
	 * funkcia rozhoduje o tom, ci sa bod nachadza v definovanom obdlzniku
	 *
	 * @param x x-ova suradnica bodu
	 * @param y y-ova suradnica bodu
	 * @param topLeft lavy horny rohovy bod obdlznika
	 * @param bottomRight pravy dolny rohovy bod obdlznika
	 * @return true ak sa bod nachadza v definovanom obdlzniku, inak false
	 */
	private static boolean isContained(float x, float y, RelativePoint topLeft,
					RelativePoint bottomRight) {
		return (x >= topLeft.x && x <= bottomRight.x)
						&& (y >= topLeft.y && y <= bottomRight.y);
	}

	/**
	 * Generuje strom sablon a testov.
	 */
	public void generateTestTree() {
		// root
		javax.swing.tree.DefaultMutableTreeNode treeNodeRoot = new javax.swing.tree.DefaultMutableTreeNode(
						"Testy");
		ArrayList<TestTemplate> templates = Exporter.getTestTemplates();
		HashSet<Test> tests = Session.getTests();
		for (TestTemplate template : templates) {
			javax.swing.tree.DefaultMutableTreeNode treeNodeTemplate = new javax.swing.tree.DefaultMutableTreeNode(
							template);
			for (Test t : tests) {
				if (t.getTemplate().getId() != template.getId()) {
					continue;
				}
				javax.swing.tree.DefaultMutableTreeNode treeNodeTest = new javax.swing.tree.DefaultMutableTreeNode(
								t);
				treeNodeTemplate.add(treeNodeTest);
			}
			treeNodeRoot.add(treeNodeTemplate);
		}
		treeTests.setModel(new javax.swing.tree.DefaultTreeModel(treeNodeRoot));
		treeTests.getSelectionModel().setSelectionMode(
						TreeSelectionModel.SINGLE_TREE_SELECTION);
	}
}
